-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
75 lines (61 loc) · 1.69 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
mod utils;
fn snafu_to_dec(n: String) -> i64 {
let mut mult = 1;
let mut dec = 0;
for c in n.chars().rev() {
match c {
'=' => dec -= mult * 2,
'-' => dec -= mult,
'0' => (),
'1' => dec += mult,
'2' => dec += mult * 2,
_ => panic!("Invalid character in SNAFU number!"),
}
mult *= 5;
}
dec
}
fn dec_to_snafu(n: i64) -> String {
let mut snafu = String::new();
let mut dec = n;
while dec > 0 {
let mut add = 0;
match dec % 5 {
0 => snafu = format!("0{}", snafu),
1 => snafu = format!("1{}", snafu),
2 => snafu = format!("2{}", snafu),
3 => {
snafu = format!("={}", snafu);
add = 1;
}
4 => {
snafu = format!("-{}", snafu);
add = 1;
}
_ => panic!("Invalid decimal number!"),
}
dec /= 5;
dec += add;
}
snafu
}
fn main() {
let input_file = utils::input_file_path();
if let Err(e) = input_file {
println!("{}", e);
return;
}
let mut input: Vec<String> = Vec::new();
if let Ok(lines) = utils::read_lines(input_file.unwrap()) {
for line in lines {
if let Ok(readed_line) = line {
input.push(readed_line);
}
}
}
let decimals = input.iter().map(|x| snafu_to_dec(x.to_string())).collect::<Vec<i64>>();
let sum = decimals.iter().sum::<i64>();
let snafu = dec_to_snafu(sum);
println!("Part 1: {}", snafu);
println!("Part 2: {}", "Not today :)");
}